package cn.disicn.controller;

import org.apache.commons.lang3.StringUtils;
import cn.disicn.common.Const;
import cn.disicn.common.ServerResponse;
import cn.disicn.model.AppUser;
import com.jfinal.core.Controller;

/**
 * @author zjk
 * @date 2017年12月29日 上午10:23:56
 * @desc app用户管理
 * 
 */
public class AppUserController extends Controller {

	private static final AppUser appUser = new AppUser();

	/**
	 * 
	 * @desc 查询app端用户
	 */
	public void index() {
		Integer pageNum = getParaToInt("pageNum");
		Integer pageSize = getParaToInt("pageSize");
		String status = getPara("status");
		if (pageNum != null && pageSize != null && pageNum > 0 && pageSize > 0) {
			renderJson(appUser.getAppUserList(pageNum, pageSize, status));
			return;
		}
		renderJson(ServerResponse
				.createByErrorMsg(Const.ErrorResult.PARAMETER_ERROR));
	}

	/**
	 * 
	 * @desc 冻结app用户账号
	 */
	public void updateStatus() {
		String id = getPara("id");
		String type = getPara("type");
		if (!StringUtils.isBlank(id) && !StringUtils.isBlank(type)
				&& id.length() == 32) {
			Integer code = Const.UserStatusInfoEnum.statusValueOf(type)
					.getCode();
			if (code != null) {
				if (code == Const.UserStatusInfoEnum.FROZEN_BIZ_JOBS.getCode()) {
					appUser.setStatus(Const.UserStatusInfoEnum.FROZEN_BIZ_JOBS
							.getCode() + "");
				} else if (code == Const.UserStatusInfoEnum.THAW_BIZ_JOBS
						.getCode()) {
					ServerResponse<String> response = appUser
							.getAuditStatus(id);
					if (response != null) {
						if (response.isSuccess()) {
							appUser.setStatus(Const.UserStatusEnum.AUTHENTICATED
									.getCode() + "");
						} else {
							appUser.setStatus(Const.UserStatusEnum.NOT_CERTIFIED
									.getCode() + "");
						}
					}
				}
				appUser.setId(id);
				renderJson(appUser.updateStatus(appUser));
				return;
			}
		}
		renderJson(ServerResponse
				.createByErrorMsg(Const.ErrorResult.PARAMETER_ERROR));
	}
}
